#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author   : chancemate
# @DateTime : 2023/3/31
# @FileName : 1
# @SofrWare : PyCharm
# @User     : 彬宇
import os, json, random, requests, re, datetime, time
import pandas as pd
from fuzzywuzzy import fuzz

"""
项目简介：
    1. 利用fuzzywuzzy，给予两个字符串进行相似度对比
    2. 数据进行处理，例如：csv【read_excel修改成read_csv】，excel
    3. 数据返回excel表格
    4. 匹配A,B,C,D列，利用B列对比A列，相似度大于80，返回C列值，填充到D空白处
"""

class Solution:
    def __init__(self, file, save_path, simi_num=80, sheet_name=0):
        self.simi_num = simi_num
        self.file = file
        self.sheet_name = sheet_name
        self.save_path = save_path
        self.df = None

    def handle_simil(self, row):
        max_similarity = 0
        max_indes = -1
        for indes, item in enumerate(self.df['B']):
            similarity = fuzz.ratio(str(row['A']), str(item))
            if similarity > max_similarity: max_similarity = similarity; max_indes = indes
        return self.df.iloc[max_indes]['C'] if max_similarity > self.simi_num else '未匹配'

    def read_csv_data(self):
        return  pd.read_excel(self.file, sheet_name=self.sheet_name)


    def run_main(self):
        df = self.read_csv_data()
        self.df = df
        df['D'] = df.apply(self.handle_simil, axis=1)
        save_file = os.path.join(self.save_path, '数据增加.xlsx')
        df.to_excel(save_file, index=False)